/*
 * BookingCommandGroup.java
 *
 * Created on October 5, 2007, 12:29 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package drb;

import java.io.BufferedReader;
import java.sql.*;

/**
 *
 * @author Asger
 */
public class BookingCommandGroup extends PromptingCommand {
    
    private FindSeatsCommand findSeatsCmd = new FindSeatsCommand(input);
    private BookSeatCommand bookSeatCmd = new BookSeatCommand(input, findSeatsCmd);
    private FindNeighboursCommand findNeighboursCmd = new FindNeighboursCommand(input, findSeatsCmd);
    private TicketInfoCommand infoCmd = new TicketInfoCommand();
    private Ticket ticket;
    
    /** Creates a new instance of BookingCommandGroup */
    public BookingCommandGroup(BufferedReader input) {
        super(input);
    }
    
    public void setConnection(Connection connection) {
        super.setConnection(connection);
        findSeatsCmd.setConnection(connection);
        bookSeatCmd.setConnection(connection);
        findNeighboursCmd.setConnection(connection);
        infoCmd.setConnection(connection);
    }
    
    public void printHelp() {
        System.out.println("-------------------------------");
        System.out.println("Booking commands for ticket #"+ticket.id+":");
        System.out.println(" find            | Finds free seats on the route for the ticket");
        System.out.println(" book            | Books a seat found using find-seats, on one train");
        System.out.println(" find-neighbours | Finds free seats near a seat reserved for this ticket");
        System.out.println(" info            | Prints information about this ticket");
        System.out.println(" back            | Returns to the root menu");
        System.out.println(" help            | Show this help");
        System.out.println("-------------------------------");
    }
    
    public void execute() throws SQLException {
        ticket = promptTicket();
        ticket.fetchTrainRoute(connection);
        ticket.fetchOwner(connection);
        
        findSeatsCmd.ticket = ticket;
        bookSeatCmd.ticket = ticket;
        findNeighboursCmd.ticket = ticket;
        infoCmd.ticket = ticket;
        
        printHelp();
        
        while (true) {
            
            try
            {
                String cmd = getInput();
                if (cmd.equals("find")) {
                    findSeatsCmd.execute();
                } else if (cmd.equals("book")) {
                    bookSeatCmd.execute();
                    findSeatsCmd.reset();
                } else if (cmd.equals("find-neighbours")) {
                    findNeighboursCmd.execute();
                    findSeatsCmd.reset();
                } else if (cmd.equals("info")) {
                    infoCmd.execute();
                    findSeatsCmd.reset();
                } else if (cmd.equals("back")) {
                    findSeatsCmd.reset();
                    break;
                } else if (cmd.equals("help")) {
                    printHelp();
                } else {
                    System.err.println("Unknown command. Type 'help' for a list of commands.");
                }
            } catch (CancelCommandException ex) {
                printHelp();
                // Catch the "cancel" command"
            }
        }
    }
}








